package com.yoyosys.echo.manager.cluster.service.impl;

import com.alibaba.fastjson.JSON;
import com.yoyosys.echo.manager.cluster.dao.EExtLineage2Mapper;
import com.yoyosys.echo.manager.system.common.Message;
import com.yoyosys.echo.manager.cluster.dao.ApiInfoMapper;
import com.yoyosys.echo.manager.cluster.po.ApiInfo;
import com.yoyosys.echo.manager.cluster.po.EExtLineage;
import com.yoyosys.echo.manager.cluster.vo.ApiInfoVo;
import com.yoyosys.echo.manager.cluster.vo.DependencyApi;
import com.yoyosys.echo.manager.cluster.service.ApiInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ApiInfoServiceImpl implements ApiInfoService {
    @Autowired
    private ApiInfoMapper apiInfoMapper;
    @Autowired
    private EExtLineage2Mapper eExtLineageMapper;

    @Override
    public Message apiinfo(ApiInfoVo apiInfo) {
        Message message = new Message();
        try {
            ApiInfo apiInfos = new ApiInfo();
            apiInfos.setApiCode(apiInfo.getApiCode());
            apiInfos.setApiLabel(apiInfo.getApiLabel());
            apiInfos.setApiDescr(apiInfo.getApiDescr());
            apiInfos.setCreateUser(apiInfo.getCreateUser());
            apiInfos.setCreateDt(apiInfo.getCreateDt());
            apiInfos.setTeamName(apiInfo.getTeamId());
            apiInfos.setTeamLabel(apiInfo.getTeamLabel());
            apiInfos.setEffDt(apiInfo.getEffDt());
            apiInfos.setExpDt(apiInfo.getExpDt());
            apiInfos.setRequestId(apiInfo.getRequestId());
            // 处理依赖关系

            List<EExtLineage> eExtLineages=new ArrayList<>();
            if (apiInfo.getDependencys() != null) {
                List<DependencyApi> dependencies = apiInfo.getDependencys().stream()
                        .map(dto -> {
                            List<EExtLineage> eExtLineageInfo=EExtLineageInfo(dto, apiInfo);
                            eExtLineages.addAll(eExtLineageInfo);
                            DependencyApi dependency = new DependencyApi();
                            dependency.setFroms(dto.getFroms());
                            dependency.setJobId(dto.getJobId());
                            dependency.setOptionDesc(dto.getOptionDesc());
                            dependency.setTo(dto.getTo());
                            dependency.setTo_system(dto.getTo_system());
                            return dependency;
                        })
                        .collect(Collectors.toList());
                apiInfos.setDependencys(JSON.toJSONString(dependencies));
            }
            ApiInfo apiInfoData =apiInfoMapper.selectApiinfo(apiInfo.getApiCode());
            if(apiInfoData != null){
                apiInfoMapper.updateApiinfo(apiInfos);
                eExtLineageMapper.delExtLineage(apiInfo.getApiCode());
            }else {
                apiInfoMapper.apiinfo(apiInfos);
            }
            eExtLineageMapper.insrteExtLineage(eExtLineages);

            message.setSuccessMsg("API基本信息提交成功");
            message.setData(apiInfos.getRequestId());
            message.setCode("200");
        }catch (Exception e){
            message.setErrorMsg("API基本信息提交失败");
            message.setCode("500");
            message.setData(e.getMessage());
        }
        return message;
    }

    private List<EExtLineage> EExtLineageInfo(DependencyApi dto, ApiInfoVo apiInfo) {
        List<EExtLineage> eExtLineages = new ArrayList<>();

       if (dto.getFroms() != null && !dto.getFroms().isEmpty()){
           List<EExtLineage> collect = dto.getFroms().stream().map(from -> {
               EExtLineage extLineage = new EExtLineage();
               String[] split = from.split("\\.");
               extLineage.setFromDbName(split[0]);
               extLineage.setFromTableName(split[1]);
               extLineage.setJobId(apiInfo.getApiCode());
               extLineage.setJobName(apiInfo.getApiLabel());
               extLineage.setFromDbType("dataFuse");
               extLineage.setFromApp("dataFuse");
               extLineage.setToApp("dataFuse");
               extLineage.setToDbType(dto.getTo_system());
               extLineage.setSourceType("EXTERNAL");
               extLineage.setSourceApp("sjdz_gongxiangpingtai");
               extLineage.setCreateTime(new Date());
               extLineage.setUpdateTime(new Date());
               extLineage.setJobDesc(apiInfo.getApiDescr());
               extLineage.setSourceTeamId(apiInfo.getTeamId());
               extLineage.setSourceTeamName(apiInfo.getTeamLabel());
               if (dto.getTo() != null && !dto.getTo().isEmpty() && dto.getTo().contains(".")) {
                   String[] splitTo = dto.getTo().split("\\.");
                   extLineage.setToDbName(splitTo[0]);
                   extLineage.setToTableName(splitTo[1]);
               }
               return extLineage;
           }).collect(Collectors.toList());
           eExtLineages.addAll(collect);
       }else{
           EExtLineage extLineage = new EExtLineage();
           extLineage.setJobId(apiInfo.getApiCode());
           extLineage.setJobName(apiInfo.getApiLabel());
           extLineage.setFromDbType("dataFuse");
           extLineage.setFromApp("dataFuse");
           extLineage.setToApp("dataFuse");
           extLineage.setToDbType(dto.getTo_system());
           extLineage.setSourceType("EXTERNAL");
           extLineage.setSourceApp("sjdz_gongxiangpingtai");
           extLineage.setCreateTime(new Date());
           extLineage.setUpdateTime(new Date());
           extLineage.setJobDesc(apiInfo.getApiDescr());
           extLineage.setJobDesc(apiInfo.getApiDescr());
           extLineage.setSourceTeamId(apiInfo.getTeamId());
           extLineage.setSourceTeamName(apiInfo.getTeamLabel());
           if (dto.getTo() != null && !dto.getTo().isEmpty() && dto.getTo().contains(".")) {
               String[] splitTo = dto.getTo().split("\\.");
               extLineage.setToDbName(splitTo[0]);
               extLineage.setToTableName(splitTo[1]);
           }
           eExtLineages.add(extLineage);
       }

       return eExtLineages;
    }


}
